package com.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.app.mapper.OptionMapper;
import com.app.mapper.QuestionnaireMapper;
import com.app.mapper.UserMapper;
import com.app.model.*;
import com.app.service.QuestionnaireService;
import com.app.vo.AnswerUserVo;
import com.app.vo.AnswerVo;
import com.app.vo.QuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class QuestionnaireServiceImpl implements QuestionnaireService {
    @Autowired
    private QuestionnaireMapper questionnaireMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OptionMapper optionMapper;

    @Override
    public QuestionVo getAll() {
        QuestionVo vo = new QuestionVo();
        List<QuestionModel> questionName = getQuestionName();
        for (int i = 0; i < questionName.size(); i++) {
            int id = questionName.get(i).getId();
            List<OptionModel> optionById = getOptionById(id);
            questionName.get(i).setOption(optionById);
        }
        vo.setQuestion(questionName);
        return vo;
    }

    @Override
    public void saveOption(JSONArray jsonArray, Integer userId) {
        List<AnswerModel> list = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            AnswerModel model = new AnswerModel();
            StringBuilder sb = new StringBuilder();
            JSONObject json = jsonArray.getJSONObject(i);
            Integer questionId = (Integer) json.get("id");
            model.setUserId(userId);
            model.setQuestionId(questionId);
            JSONArray optionArray = JSONArray.parseArray(json.get("option").toString());
            for (int i1 = 0; i1 < optionArray.size(); i1++) {
                JSONObject option = optionArray.getJSONObject(i1);
                String selected = option.get("selected").toString();
                Integer parentId = (Integer) option.get("pid");
                if (selected.equals("true") && Objects.equals(questionId, parentId)) {
                    String optionId = option.get("id").toString();
                    sb.append(optionId + ",");
                }
                model.setOptionId(sb.toString());
            }
            list.add(i, model);
        }
        //存入数据库
        for (int i = 0; i < list.size(); i++) {
            questionnaireMapper.insert(list.get(i));
        }
    }

    @Override
    public AnswerUserVo selectAnswer(Integer page, Integer limit, String keyword) {
        //查所有提交的问卷
        List<AnswerModel> answerModelList = questionnaireMapper.selectAnswer();
        LinkedHashSet<AnswerModel> linkedHashSet = new LinkedHashSet<>();
        for (int i = 0; i < answerModelList.size(); i++) {
            AnswerModel model = new AnswerModel();
            Integer userId = answerModelList.get(i).getUserId();
            UserModel userModel = userMapper.selectUser(userId,keyword);
            if (userModel==null){
                continue;
            }
            model.setId(userId);
            model.setMobile(userModel.getMobile());
            model.setUserName(userModel.getUserName());
            model.setFaceUrl(userModel.getFaceUrl());
            model.setCreateTime(answerModelList.get(i).getCreateTime());
            linkedHashSet.add(model);
        }
        List<AnswerModel> collect = linkedHashSet.stream().skip((long) (page - 1) * limit).limit(limit).collect(Collectors.toList());
        AnswerUserVo vo = new AnswerUserVo();
        vo.setCount(linkedHashSet.size());
        vo.setList(collect);
        return  vo;
    }

    @Override
    public AnswerVo selectAnswerContent(Integer userId, Integer page, Integer limit) {
        List<AnswerModel> answerModels = questionnaireMapper.selectAnswerContent(userId);
        List<QuestionModel> list = new ArrayList<>();
        for (int i = 0; i < answerModels.size(); i++) {
            QuestionModel model = new QuestionModel();
            model.setCreatTime(answerModels.get(i).getCreateTime());
            //查用户信息
            UserModel userModel = userMapper.selectUserInFo(userId);
            model.setUser(userModel);
            //查问题
            Integer questionId = answerModels.get(i).getQuestionId();
            QuestionModel questionModel = questionnaireMapper.selectQuestion(questionId);
            model.setQuestionName(questionModel.getQuestionName());
            model.setId(questionModel.getId());
            model.setChoice(questionModel.getChoice());
            List<String> optionList = new ArrayList<>();
            String optionId = answerModels.get(i).getOptionId();
            String[] split = optionId.split(",");
            //查选项
            for (int i1 = 0; i1 < split.length; i1++) {
                Integer integer = Integer.valueOf(split[i1]);
                String s = questionnaireMapper.selectOption(integer);
                optionList.add(i1, s);
            }
            model.setOptionName(optionList);
            list.add(i, model);
        }
        //当前页  每页条数 总数
        //对list集合进行分页
        List<QuestionModel> collect = list.stream().skip((long) (page - 1) * limit).limit(limit).collect(Collectors.toList());
        AnswerVo vo = new AnswerVo();
        vo.setCount(list.size());
        vo.setList(collect);
        return vo;
    }

    @Override
    public void insertQuestionAndOption(OptionModel model) {
        optionMapper.insert(model);
    }

    @Override
    public void updateQuestionAndOption(OptionModel model) {
        optionMapper.updateById(model);
    }

    @Override
    public void deleteQuestionAndOption(Integer id) {
        optionMapper.deleteById(id);
    }

    @Override
    public List<QuestionModel> selectQuestion(String name) {
        List<QuestionModel> questionName = selectQuestionName(name);
        for (int i = 0; i < questionName.size(); i++) {
            if (questionName.get(i).getChoice() == 1) {
                questionName.get(i).setChoiceText("是");
            } else {
                questionName.get(i).setChoiceText("否");
            }
            int id = questionName.get(i).getId();
            List<OptionModel> optionById = selectOptionById(id);
            questionName.get(i).setChild(optionById);
        }
        return questionName;
    }

    @Override
    public void updateStatus(Integer status, Integer id) {
        questionnaireMapper.updateStatus(status, id);
    }

    @Override
    public int getCount() {
        return questionnaireMapper.getCount();
    }

    private List<OptionModel> selectOptionById(int id) {
        return questionnaireMapper.selectOptionById(id);
    }

    private List<QuestionModel> selectQuestionName(String name) {
        return questionnaireMapper.selectQuestionName(name);
    }


    private List<OptionModel> getOptionById(int id) {
        return questionnaireMapper.getOptionById(id);
    }

    private List<QuestionModel> getQuestionName() {
        return questionnaireMapper.getQuestionName();
    }
}
